package cn.stylefeng.guns.modular.yingbao.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.yingbao.entity.SysService;
import cn.stylefeng.guns.modular.yingbao.mapper.SysServiceMapper;
import cn.stylefeng.guns.modular.yingbao.model.params.SysServiceParam;
import cn.stylefeng.guns.modular.yingbao.model.result.SysServiceResult;
import  cn.stylefeng.guns.modular.yingbao.service.SysServiceService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 客服 服务实现类
 * </p>
 *
 * @author zs
 * @since 2019-12-19
 */
@Service
public class SysServiceServiceImpl extends ServiceImpl<SysServiceMapper, SysService> implements SysServiceService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(SysServiceParam param){
        SysService entity = getEntity(param);
        this.save(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(SysServiceParam param){
        this.removeById(getKey(param));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysServiceParam param){
        SysService oldEntity = getOldEntity(param);
        SysService newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public SysServiceResult findBySpec(SysServiceParam param){
        return null;
    }

    @Override
    public List<SysServiceResult> findListBySpec(SysServiceParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(SysServiceParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public ResponseData queryAll() {
        List<SysService> sysServices = this.baseMapper.selectList(new QueryWrapper<>());
        if(sysServices.size() != 0){
            return ResponseData.success(sysServices);
        }
        return ResponseData.error(500,"暂无数据");
    }

    private Serializable getKey(SysServiceParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private SysService getOldEntity(SysServiceParam param) {
        return this.getById(getKey(param));
    }

    private SysService getEntity(SysServiceParam param) {
        SysService entity = new SysService();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }
}
